/*

Input: 	
	> $tmp/raw_stimmbez_all_elections.dta [precinct-level data with year-specific precinct]
	> $tmp/panel_wahlen.dta [Address-level election data with year-specific boundaries]
	> $tmp/convkeys_ltw2018.dta [Conversion keys]
				
Output: 	$tmp/panel_wahlen_covted_ltw18.dta [Address-level election data with boundaries as of SE-18]

				
Main tasks:
	A) Convert Election-specific variables to precinct boundaries as of SE-18 and
	B) assign to each address its precinct as of SE-18.


Note: 	* Macro "sb_vars" defined below, contains precinct-level variables to be
			converted => adjust if necessary

		* sb_new := precinct as of SE-18
		* sb_old := Original precinct
		
*/


*** Define precinct-Variables to be converted
// NB: These variables will be SUMMED
global sb_vars  wahlber_gesamt wahlber_ohne_wahlschein wahlber_mit_wahlschein ///
				wahlber_nicht_im_waehlerv waehler_gesamt waehler_mit_wahlschein ///
				ungueltige_stimmen ew_* wb_* hh_sgl hh_kids avg_dur anz_* kow_stimmenges	

/// A) Convert precinct-level Data for all elections ///

* 1) Pull precinct-level data for all elections
	use "$tmp/raw_stimmbez_full.dta", clear
	

* 2) Merge w/ conversion keys
*	 NB: Only Levels can be converted, no shares!
	merge 1:n wahl sb using "$tmp/convkeys_ltw2018.dta", keep(1 3)
		
		assert wahl == "LTW18" 	if _merge == 1
		assert _merge ==1 			if wahl == "LTW18"
		assert !missing(conv_wgt) 	if wahl != "LTW18"
		drop _merge
	
	* gen pseudo key for LTW 2018
	replace conv_wgt = 1 	if wahl == "LTW18"
	replace sb_ltw2018 = sb if wahl == "LTW18"
	

	
* 3) Convert variables
	foreach v of varlist $sb_vars {
		gen covted_`v' = `v' * conv_wgt
		local lb : variable label `v'
		lab var covted_`v' "`lb'"
		drop `v'
	}
	

* 4) Collapse (SUM) to new precinct

	// copy labels
	foreach v of varlist * {
		local x = subinstr("`v'","covted_","",.)
		local `x'_lb : variable label `v'
	}
	
	collapse (sum) covted_*, by(wahl sb_ltw2018) fast

	rename covted_* *
	
	// re-label						
	foreach v of varlist * {
		lab var `v' "``v'_lb'"
	}


* 5) Save precinct x election panel w/ boundaries as of 2018

	* rename: sb_new = sb as of SE-18
	rename 	sb_ltw2018 sb_new
	
	* gen year variable
	gen 	jahr =  "20"+substr(wahl,-2,.)
	destring jahr, replace
	lab var jahr "Election year"
	
	* save converted precinct-data
	save "$tmp/stimmbez_all_elections_covted_ltw18.dta", replace
	
	
/// B) Convert Address-level data (8 elections) and merge with precinct-data ///

* 1) Pull original address-level election data
	use "$tmp/panel_wahlen.dta", clear
		
* 1.1) Assign to each address precinct as of SE-18
	
	// Note: district (stadtbez) doesn't change over time
	bys voter_id (stadtbez): assert stadtbez[1] == stadtbez[_N]
	
	// gen sb_new := precinct as of SE-18
	gen tmp = sb if wahl == "LTW18"
	bys voter_id (tmp): gen sb_new = tmp[1]
	lab var sb_new 		"Precinct as of SE-18"
	assert !missing(sb_new)
	assert sb_new==sb if wahl_id==6
	drop tmp*
	
	* mark old precinct
	rename 	sb sb_old
	lab var sb_old "Original precinct of voter_id"
		
* 2) Merge converted precinct oucomes to addresses (master-only = FE-09)			
	merge n:1 wahl sb_new using "$tmp/stimmbez_all_elections_covted_ltw18.dta", assert(1 3)
	assert wahl == "BTW09" if _merge == 1
	drop _merge
	
	
	order voter_id jahr wahl sb_new sb_old

* 3) Convert Strukturindikatoren back to shares
		// Share anhand von EINWOHNER (residents)
		*->Note: household level variables included
	foreach v of varlist ew_dt ew_ausl* ew_mihi ew_ledig ew_married hh_sgl hh_kids{
		replace `v' = `v' /ew_ges
		assert inrange(`v',0,1) if wahl_id!= 0 // "FE-09"
	}

	// Share given eligible voters
	foreach v of varlist wb_18t24 wb_25t34 wb_35t44 wb_45t59 wb_60plus wb_dt wb_ausl{
		replace `v' = `v' / wahlber_gesamt
		assert inrange(`v',0,1) if wahl_id != 0 // "FE-09"
	
	}
	
* 4) Generate shares of party outcomes
	foreach v of varlist anz_* {
		local partei = subinstr("`v'","anz_","",.)	
		gen 	shr_`partei' = `v'/(waehler_gesamt-ungueltige_stimmen) if !inlist(wahl, "KOW20", "KOW14")
		replace shr_`partei' = `v'/kow_stimmenges if inlist(wahl, "KOW20", "KOW14")
		replace shr_`partei' = 0 if missing(shr_`partei')
		lab var shr_`partei' "Party share of `partei' (in-person)"
	}

	
* 5) Finalize and save
	
 * GEN wahl_type
	gen tmp = substr(wahl,1,3)
	encode tmp, gen(wahl_type)
	lab var wahl_type "Election type ID"

	// save
	order 	voter_id wahl wahl_type jahr sb_new sb_old
	compress
	cap drop tmp*
	isid voter_id wahl_id
	save "$tmp/panel_wahlen_covted_ltw18.dta", replace
